//
//  Extension.swift
//  YYPBlankView
//
//  Created by YYPan on 2018/5/18.
//  Copyright © 2018年 YYPan. All rights reserved.
//

import UIKit
import SnapKit

extension UIView {
	
	enum YYPBlankViewType {
		case noNetWork
		case noSearchResults
		case noResults
	}
	
	/*
	extension中可以扩展计算属性但是不能扩展存储属性,可以利用runtime运行时来进行存储属性的扩展
	objc_getAssociatedObject
	objc_setAssociatedObject
	*/
	
	private struct RuntimeKey {
		static let blankViewKey = UnsafeRawPointer.init(bitPattern: "blankViewKey".hashValue)
		static let reloadKey = UnsafeRawPointer.init(bitPattern: "reloadKey".hashValue)
	}
	
	private var blankView: UIView? {
		get {
			return objc_getAssociatedObject(self, RuntimeKey.blankViewKey!) as? UIView
		}
		set {
			objc_setAssociatedObject(self, RuntimeKey.blankViewKey!, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
		}
	}
	
	private var reloadBlock: (() -> ())? {
		get {
			return objc_getAssociatedObject(self, RuntimeKey.reloadKey!) as? (() -> ())
		}
		set {
			objc_setAssociatedObject(self, RuntimeKey.reloadKey!, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
		}
	}
	
	func showBlankView(blankViewType: YYPBlankViewType, reloadBlock: (() -> ())?) {
		
		self.reloadBlock = reloadBlock
		
		// 显示占位图时禁止view滑动
		if self.isKind(of: UIScrollView.self) {
			let scrollView = self as! UIScrollView
			scrollView.isScrollEnabled = false
		}
		
		if self.blankView != nil {
			self.blankView?.removeFromSuperview()
			self.blankView = nil
		}
		
		self.blankView = UIView()
		self.addSubview(self.blankView!)
		self.blankView?.backgroundColor = UIColor.white
		self.blankView?.snp.makeConstraints({ (make) in
			make.center.equalTo(self)
			make.size.equalTo(self)
		})
		
		let imageView = UIImageView()
		imageView.contentMode = .scaleAspectFit
		self.blankView?.addSubview(imageView)
		imageView.snp.makeConstraints { (make) in
			make.centerX.equalTo(self)
			make.centerY.equalTo(self.snp.centerY).offset(-50)
			make.size.equalTo(CGSize(width: 130, height: 100))
		}
		
		let label = UILabel()
		label.textAlignment = .center
		label.font = UIFont.systemFont(ofSize: 16)
		label.textColor = UIColor.black
		self.blankView?.addSubview(label)
		label.snp.makeConstraints { (make) in
			make.centerX.equalTo(self)
			make.top.equalTo(imageView.snp.bottom).offset(15)
			make.size.equalTo(CGSize(width: self.frame.size.width - 40, height: 20))
		}
		
		let button = UIButton()
		button.titleLabel?.font = UIFont.systemFont(ofSize: 15)
		button.setTitleColor(UIColor.black, for: .normal)
		button.addTarget(self, action: #selector(reloadAction), for: .touchUpInside)
		button.layer.borderWidth = 0.5
		button.layer.borderColor = UIColor.gray.cgColor
		button.layer.cornerRadius = 5
		self.blankView?.addSubview(button)
		button.snp.makeConstraints { (make) in
			make.centerX.equalTo(self)
			make.top.equalTo(label.snp.bottom).offset(15)
			make.size.equalTo(CGSize(width: 80, height: 20))
		}
		
		switch blankViewType {
		case .noNetWork:
			imageView.image = UIImage(named: "当前没有网络")
			label.text = "网络出错啦~"
			button.setTitle("点击重试", for: .normal)
		case .noSearchResults:
			imageView.image = UIImage(named: "搜索-空")
			label.text = "没有找到您要的结果哦~"
			button.removeFromSuperview()
		case .noResults:
			imageView.removeFromSuperview()
			label.text = "没有数据"
			label.snp.remakeConstraints({ (make) in
				make.centerX.equalTo(self)
				make.centerY.equalTo(self).offset(-27.5)
				make.size.equalTo(CGSize(width: self.frame.size.width - 40, height: 20))
			})
			button.setTitle("点击刷新", for: .normal)
		}
	}
	
	func removeBlankView() {
		if self.blankView == nil {
			return
		}
		OperationQueue.main.addOperation {
			self.blankView?.removeFromSuperview()
			self.blankView = nil
			
			if self.isKind(of: UIScrollView.self) {
				let scrollView = self as! UIScrollView
				scrollView.isScrollEnabled = true
			}
		}
	}
	
	@objc private func reloadAction() {
		reloadBlock?()
	}
}
